/**
 * Created by zhangyilin on 2018/11/6.
 */
import java.sql.*;
public class DBcoon {
    Connection conn = null;
    Statement stmt = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    String url = "jdbc:mysql://localhost:3306/ListSystem";
    String userName = "root";
    String password = "";
    String driverName = "com.mysql.jdbc.Driver";
    public DBcoon(){
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url,userName, password);// 连续数据库
            if(!conn.isClosed())
                System.out.println("Succeeded connecting to the Database!");
        }catch (Exception e){e.printStackTrace();}
    }

    public boolean executeUpdate(String sql){//更新
        try {
            conn = DriverManager.getConnection(url,userName,password);
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return true;
    }
    public ResultSet executeQuery(String sql){//查询
        rs = null;
        try {
            conn = DriverManager.getConnection(url,userName,password);
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return rs;
    }

    public int insert(String sql) {//插入一条数据
        int count = 0;
        try {
            conn = DriverManager.getConnection(url,userName,password);
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            count = stmt.executeUpdate(sql);
            if (count != 0) conn.commit();
            else conn.rollback();
            stmt.close();
            System.out.println("执行结束");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return count;
    }

}
